package com.xc.app.configs.shiro;

import com.xc.app.domain.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class UserRealm extends AuthorizingRealm {

    @Override
    public String getName() {
        return "userRealm";
    }

    /**
     * 授权
     *
     * @title: doGetAuthorizationInfo
     * @author: 七天
     * @date: 2018/3/13 9:29
     * @param: [principals]
     * @return: org.apache.shiro.authz.AuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    /**
     * 权限认证
     *
     * @title: doGetAuthenticationInfo
     * @author: 七天
     * @date: 2018/3/13 9:29
     * @param: [token]
     * @return: org.apache.shiro.authc.AuthenticationInfo
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        User user = new User();
        user.setLoginName("admin");
        user.setPassword("928bfd2577490322a6e19b793691467e"); //123456
        ByteSource byteSource = ByteSource.Util.bytes(user.getLoginName());
        return new SimpleAuthenticationInfo(user, user.getPassword(), byteSource, getName());
    }

    public static void main(String[] args) {
        Object obj = new SimpleHash("md5", "123456", "admin", 2);
        System.out.println(obj);
    }
}
